package com.example.controller;


import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.*;
import com.example.service.IFamilyService;
import com.example.service.IFirstService;
import com.example.service.IIncomeService;
import com.example.service.ISecondService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 收入表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-29
 */
@RestController
@RequestMapping("/income")
@Slf4j
@RequiredArgsConstructor
@CrossOrigin
public class IncomeController {

    private final IIncomeService iIncomeService;

    private final IFirstService iFirstService;

    private final ISecondService iSecondService;

    private final IFamilyService iFamilyService;

    @GetMapping("/page")
    public Result<PageResult> page(IncomePage incomePage){
        Page<Income> page = Page.of(incomePage.getPage(),incomePage.getPageSize());
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("time");
        orderItem.setAsc(false);
        page.addOrder(orderItem);
        iIncomeService.lambdaQuery()
                .eq(incomePage.getTypeKey() != null,Income::getTypeKey,incomePage.getTypeKey())
                .like(incomePage.getDescription() != null,Income::getDescription,incomePage.getDescription())
                .eq(incomePage.getMember() != null,Income::getMember,incomePage.getMember())
                .between(incomePage.getBegin() != null && incomePage.getEnd() != null,Income::getTime,incomePage.getBegin(),incomePage.getEnd())
                .page(page);
        List<IncomeVO> list = page.getRecords().stream().map(income -> {
            IncomeVO incomeVO = new IncomeVO();
            BeanUtils.copyProperties(income, incomeVO);
            incomeVO.setTypeName(iFirstService.getById(income.getTypeKey()).getTypeName());
            incomeVO.setMemberName(iFamilyService.getById(income.getMember()).getName());
            return incomeVO;
        }).collect(Collectors.toList());
        return Result.success(new PageResult(page.getTotal(),list));

    }

    @PutMapping
    public Result update(@RequestBody Income income){
        log.info("进行更新收支操作：{}",income);
        iIncomeService.updateById(income);
        return Result.success();
    }

    @PostMapping("/save")
    public Result save(@RequestBody Income income){
        income.setCreateTime(LocalDateTime.now());
        iIncomeService.save(income);
        return Result.success();
    }

    @DeleteMapping("/delete")
    public Result delete(Long id){
        iIncomeService.removeById(id);
        return Result.success();
    }

    @GetMapping("/detailedIncome")
    public Result details(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                          @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        List<Double> incomes = iIncomeService.details(begin,end);
        return Result.success(incomes);
    }

    @GetMapping("/classifiedIncome")
    public Result<List<CIncome>> classified(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("传入的参数是：{},{}",begin,end);
        List<CIncome> list = iIncomeService.classified(begin,end);
        return Result.success(list);
    }

}
